import { observable, action, makeAutoObservable, runInAction } from 'mobx'
import { findIndex, find } from 'lodash'

class TimeArea {
  constructor() {
    makeAutoObservable(this)
  }

  // 每个分屏的时间对象
  @observable timeArray = []

  @action addTimeArray(id, { start, now, end }) {
    runInAction(() => {
      this.timeArray.push({ id, start, end, now })
    })
  }
  @action getTimeById(id) {
    return find(this.timeArray, ['id', id])
  }
  @action setTimeArray(id, str, val) {
    const time = find(this.timeArray, ['id', id])
    if (time) {
      time[str] = val
    }
  }
  @action changeTimeArray(id, time) {
    const index = findIndex(this.timeArray, ['id', id])
    if (index !== -1) {
      this.timeArray.splice(index, 1)
      this.timeArray.push({ id, ...time })
    }
  }
}

export default new TimeArea()
